package com.blb.service;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blb.entity.Role;
import com.blb.entity.User;
import com.blb.entity.UserExample;
import com.blb.entity.UserExample.Criteria;
import com.blb.entity.Userrole;
import com.blb.entity.UserroleExample;
import com.blb.mapper.RoleMapper;
import com.blb.mapper.UserMapper;
import com.blb.mapper.UserroleMapper;
import com.blb.utils.LayuiPage;
import com.mysql.jdbc.StringUtils;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserroleMapper userroleMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 用户登录
     *
     * @param user
     * @return
     * @throws IOException
     */
    public String login(User user) throws IOException {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());

        List<User> list = userMapper.selectByExample(userExample);
        if (list.isEmpty()) {
            return "用户名不存在";
        }

        if (!user.getPassword().equals(list.get(0).getPassword())) {
            return "密码输入错误";
        }
        return "";
    }

    /**
     * 分页
     *
     * @param page
     * @param limit
     * @param username
     * @return
     * @throws IOException
     */
    public LayuiPage page(int page, int limit, String username) throws IOException {

        //获取用户集合
        List<User> list = userMapper.page(page, limit, username);

        UserExample userExample = new UserExample();
        if (!StringUtils.isNullOrEmpty(username)) {
            Criteria criteria = userExample.createCriteria();
            criteria.andUsernameLike("%" + username + "%");
        }

        //获取总条数
        int count = (int) userMapper.countByExample(userExample);

        //生产json数据
        LayuiPage pagelist = new LayuiPage();
        pagelist.setData(list);
        pagelist.setCount(count);

        return pagelist;
    }

    /**
     * 判断用户名是否存在
     *
     * @param username
     * @throws IOException
     */
    public List<User> isExist(String username) throws IOException {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> list = userMapper.selectByExample(userExample);
        return list;
    }

    /**
     * 添加用户
     *
     * @param user
     * @param httpsession
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public String add(User user, String roles) throws IOException {
        try {
            userMapper.insertSelective(user);

            String[] role = roles.split(",");
            for (String r : role) {
                Userrole userrole = new Userrole();
                userrole.setRoleid(r);
                userrole.setUserid(user.getId());
                userroleMapper.insertSelective(userrole);
            }
        } catch (Exception e) {
            return e.getMessage();
        }

        return "";

    }

    /**
     * 修改
     *
     * @param user
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(User user, String roles) throws IOException {

        try {
            UserExample userExample = new UserExample();
            Criteria criteria = userExample.createCriteria();
            criteria.andIdEqualTo(user.getId());
            userMapper.updateByExampleSelective(user, userExample);

            UserroleExample userroleExample = new UserroleExample();
            userroleExample.createCriteria().andUseridEqualTo(user.getId());
            userroleMapper.deleteByExample(userroleExample);

            String[] role = roles.split(",");
            for (String r : role) {
                Userrole userrole = new Userrole();
                userrole.setRoleid(r);
                userrole.setUserid(user.getId());
                userroleMapper.insertSelective(userrole);
            }
        } catch (Exception e) {
            return e.getMessage();
        }
        return "";
    }

    /**
     * 删除
     *
     * @param ids
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public String delete(List<String> ids) throws IOException {
        try {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andIdIn(ids);
            userMapper.deleteByExample(userExample);

            UserroleExample userroleExample = new UserroleExample();
            userroleExample.createCriteria().andUseridIn(ids);
            userroleMapper.deleteByExample(userroleExample);

        } catch (Exception e) {
            return e.getMessage();
        }
        return "";

    }

    /**
     * 角色列表
     *
     * @return
     * @throws IOException
     */
    public List<Role> roleNameList() throws IOException {
        List<Role> roleList = roleMapper.selectByExample(null);
        return roleList;
    }

    /**
     * 根据用户ID回去用户角色
     *
     * @param user
     * @return
     * @throws IOException
     */
    public List<Userrole> getRoleListByuserid(User user) throws IOException {
        UserroleExample userroleExample = new UserroleExample();
        userroleExample.createCriteria().andUseridEqualTo(user.getId());
        List<Userrole> list = userroleMapper.selectByExample(userroleExample);
        return list;
    }

    /**
     * 设置状态
     *
     * @param state
     * @param id
     * @throws IOException
     */
    public void state(boolean state, String id) throws IOException {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(id);

        User user = new User();
        if (state) {
            user.setState(1);
        } else {
            user.setState(-1);
        }
        userMapper.updateByExampleSelective(user, userExample);
    }
}
